<template>
  <div>
    <!-- 搜索区域开始 -->
    <van-search
      v-model="value"
      show-action
      placeholder="请输入搜索关键词"
      @search="onSearch"
      @input="changeContainer"
    >
      <template #action>
        <div v-if="isShow" @click="onSearch">搜索</div>
        <div v-if="!isShow" @click="onCancel">取消</div>
      </template>
    </van-search>
    <!-- 搜索区域结束 -->
    <!-- 历史搜索区域开始 -->
    <div v-if="!isShow" class="searchHistory">
      <div class="history">
        <p style="font-size: 16px">历史搜索</p>
        <van-icon @click="deleteOne" style="font-size: 18px" name="delete" />
      </div>

      <div class="database">
        <p @click="onClickContianer" v-for="(item, index) in this.$store.state.searchList" :key="index">
          {{ item }}
        </p>
      </div>
    </div>
    <!-- 历史搜索区域结束 -->
    <!-- 搜索内容展示区域开始 -->
    <div class="wrapper" v-if="!isFalse">
      <div
        @click="detailsContianer(item.id)"
        class="container"
        v-for="(item, index) in listContainerRight"
        :key="index"               
      >25
        <p class="h2">{{ item.title }}</p>
        <p class="tiems">
          <span class="icon_font">
            <van-icon name="clock-o" />
          </span>
          <span class="times_font">123213123213</span>
        </p>
        <p class="image_img">
          <img class="img" :src="item.teachers_list[0].teacher_avatar" alt="" />
          <span class="img_span">{{ item.teachers_list[0].teacher_name }}</span>
        </p>
        <div class="bottom">
          <p class="baoming">{{ item.brows_num }}个人已经报名</p>
          <p v-if="item.price == 0" class="kaishi">免费</p>
          <p v-if="item.price != 0" class="kaishi">{{ item.price }}元</p>
        </div>
      </div>
    </div>
    <!-- 搜索内容展示区域结束 -->
    <!-- 没有数据空状态展示区域开始 -->
    <div v-if="!siShowFalse">
      <van-empty image="error" description="搜索内容不存在哦宝贝~" />
    </div>
    <!-- 没有数据空状态展示区域结束 -->
    <div></div>
  </div>
</template>

<script>
import { lglSearchListContainer } from "../../util/http";
export default {
  name: "",
  components: {},
  props: {},
  data() {
    return {
      isShow: false, // 判断是搜索还是取消
      value: "", //value
      siShowFalse: -1, //判断是空状态
      listContainerRight: [],
      isFalse:false,
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    //取消
    onCancel() {
      this.$router.go(-1);
      this.listContainerRight=[];
    },
    //确认
    onSearch() {
      if(this.value == "") {
        return false;
      }
      //拿到每次 搜索的值 首先存到vuex
      this.$store.commit("searchListContainer",this.value);
      //this.value
      let data = {
        page: 1,
        value: this.value,
      };
      lglSearchListContainer(data).then((res) => {
        
        //如果没有数据的话
        if (res.list.length == 0) {
          this.siShowFalse = false;
          return;
        }
        //有数据的话
        this.listContainerRight = res.list;
      });
    },
    //deleteOne 删除本地搜索记录
    deleteOne() {
      this.$store.commit("searchListDelete", []);
      this.listContainerRight=[];
    },
    //输入文本的时候触发
    changeContainer() {
      //判断 是搜索还是取消
      if (this.value == "") {
        this.isShow = false;
        this.siShowFalse = true;
        this.isFalse=true;
        this.listContainerRight=[];
      } else {
        this.isShow = true;
      }
    },
    //点击历史搜索 再次跳转搜索
    onClickContianer(e){
      this.listContainerRight=[];
      this.isFalse=false
      this.isShow = true;
      this.value=e.target.innerText;
      //执行确认按钮 提交数据
      this.onSearch();
    },
    //点击详情跳转页面
        detailsContianer(id) {
      this.$router.push({
        path: `/detailsPage`,
        query: {
          id,
        },
      });
    },
    
  },
};
</script>

<style scoped >
.searchHistory {
  margin-top: 30px;
  width: 100vw;
}
.history {
  display: flex;
  box-sizing: border-box;
  padding: 0 20px;
  justify-content: space-between;
}
.database {
  display: flex;
  flex-wrap: wrap;
}
.database > p {
  background: red;
  margin-left: 30px;
  margin-top: 10px;
  font-size: 18px;
  line-height: 20px;
  padding: 11px;
  border-radius: 15%;
}
.wrapper {
  width: 100vw;
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  box-sizing: border-box;
  padding-bottom: 20px;
}
.container {
  width: 85%;
  height: 200px;
  box-shadow: 0 0 0.6 #000;
  border-radius: 5%;
  margin-top: 15px;
  background: #ddd;
  padding: 30px 20px 16px;
  box-sizing: border-box;
}
/* 文字 */
.h2 {
  font-size: 18px;
}
.tiems {
  line-height: 10px;
  font-size: 16px;
  /* margin-top: -13px; */
}
.icon_font {
  float: left;
}
.times_font {
  display: block;
  float: left;
  margin: 4px;
}
.image_img {
  float: left;
}
.img {
  width: 13%;
  border-radius: 44%;
  float: left;
}
.img_span {
  display: block;
  float: left;
  margin: 13px;
  font-size: 3.2vw;
  color: rgba(0, 0, 0, 0.45);
}
.bottom {
  float: left;
  width: 100%;
  height: 40px;
  border-top: 1px solid #f5f5f5;
  margin-top: 15px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.baoming {
  font-size: 16px;
  color: rgba(0, 0, 0, 0.45);
}
.kaishi {
  font-size: 4.26667vw;
  color: #44a426;
}
.van-list {
  height: 100vh;
}
.app1 {
  width: 100vw;
  padding: 0, 0, 0, 0;
  box-sizing: border-box;
  padding-top: 88px;
}
.kong {
  width: 100vw;
  height: 60px;
}
</style>
